﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Srvtools;
using System.Collections;

namespace EEPNetFLClient
{
    public partial class frmFavorMenu : Form
    {
        public frmFavorMenu()
        {
            InitializeComponent();
        }

        private ArrayList MenuIDList = new ArrayList();
        private ArrayList CaptionList = new ArrayList();
        private ArrayList ParentList = new ArrayList();
        public bool result = false;
        public frmFavorMenu(ArrayList MenuID, ArrayList Caption, ArrayList Parent)
        {
            MenuIDList = MenuID;
            CaptionList = Caption;
            ParentList = Parent;
            InitializeComponent();
        }

        private void frmFavorMenu_Load(object sender, EventArgs e)
        {
            this.cbGroup.Items.Add("");

            object[] strParam1 = new object[2];
            strParam1[0] = CliUtils.fCurrentProject;
            strParam1[1] = "F";
            DataSet dsMenus = new DataSet();
            object[] menus = CliUtils.CallMethod("GLModule", "FetchMenus", strParam1);
            if (menus != null && Convert.ToInt16(menus[0]) == 0)
            {
                dsMenus = menus[1] as DataSet;
            }
            if (dsMenus.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < dsMenus.Tables[0].Rows.Count; i++)
                    if (dsMenus.Tables[0].Rows[i]["PARENT"] != null && dsMenus.Tables[0].Rows[i]["PARENT"].ToString() != "")
                    {
                        this.lbAll.Items.Add(dsMenus.Tables[0].Rows[i]["CAPTION"].ToString());
                    }
                    else if (dsMenus.Tables[0].Rows[i]["PACKAGE"] == null || dsMenus.Tables[0].Rows[i]["PACKAGE"].ToString() == "")
                    {
                        this.cbGroup.Items.Add(dsMenus.Tables[0].Rows[i]["CAPTION"].ToString());
                    }
            }

            DataSet dsFavorMenus = new DataSet();
            object[] strParam = new object[2];
            strParam[0] = CliUtils.fCurrentProject;
            strParam[1] = "F";
            object[] favorMenus = CliUtils.CallMethod("GLModule", "FetchFavorMenus", strParam);
            if (favorMenus != null && Convert.ToInt16(favorMenus[0]) == 0)
            {
                dsFavorMenus = favorMenus[1] as DataSet;
            }
            if (dsFavorMenus.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < dsFavorMenus.Tables[0].Rows.Count; i++)
                    this.lbFavor.Items.Add(dsFavorMenus.Tables[0].Rows[i]["CAPTION"].ToString());
            }

            //for (int i = 0; i < CaptionList.Count; i++)
            //{
            //    this.lbAll.Items.Add(CaptionList[i]);
            //}
            for (int i = 0; i < this.lbFavor.Items.Count; i++)
            {
                this.lbAll.Items.Remove(this.lbFavor.Items[i]);
            }

            cbGroup_SelectedIndexChanged(sender, e);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAddAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.lbAll.Items.Count; i++)
                this.lbFavor.Items.Add(this.lbAll.Items[i].ToString());
            this.lbAll.Items.Clear();
        }

        private void btnRemoveAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.lbFavor.Items.Count; i++)
                this.lbAll.Items.Add(this.lbFavor.Items[i].ToString());
            this.lbFavor.Items.Clear();

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            ArrayList removeList = new ArrayList();
            for (int i = 0; i < this.lbAll.SelectedItems.Count; i++)
            {
                this.lbFavor.Items.Add(this.lbAll.SelectedItems[i].ToString());
                removeList.Add(this.lbAll.SelectedItems[i].ToString());
            }
            for (int i = 0; i < removeList.Count; i++)
            {
                this.lbAll.Items.Remove(removeList[i]);
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            ArrayList removeList = new ArrayList();
            for (int i = 0; i < this.lbFavor.SelectedItems.Count; i++)
            {
                this.lbAll.Items.Add(this.lbFavor.SelectedItems[i].ToString());
                removeList.Add(this.lbFavor.SelectedItems[i].ToString());
            }
            for (int i = 0; i < removeList.Count; i++)
            {
                this.lbFavor.Items.Remove(removeList[i]);
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            ArrayList menuID = new ArrayList();
            ArrayList caption = new ArrayList();
            for (int i = 0; i < this.lbFavor.Items.Count; i++)
            {
                caption.Add(this.lbFavor.Items[i].ToString());
                menuID.Add(getMenuID(this.lbFavor.Items[i].ToString()));
            }
            object[] param = new object[5];
            param[0] = CliUtils.fLoginUser;
            param[1] = CliUtils.fCurrentProject;
            param[2] = menuID;
            param[3] = caption;
            param[4] = cbGroup.Text;

            CliUtils.CallMethod("GLModule", "GetFavorMenuID", param);
            result = true;
            this.Close();
        }

        private String getMenuID(String caption)
        {
            for (int i = 0; i < MenuIDList.Count; i++)
            {
                if (CaptionList[i].ToString() == caption)
                    return MenuIDList[i].ToString();
            }
            return "";
        }

        private void cbGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.lbFavor.Items.Clear();

            DataSet dsFavorMenus = new DataSet();
            object[] strParam = new object[2];
            strParam[0] = CliUtils.fCurrentProject;
            strParam[1] = "F";
            object[] favorMenus = CliUtils.CallMethod("GLModule", "FetchFavorMenus", strParam);
            if (favorMenus != null && Convert.ToInt16(favorMenus[0]) == 0)
            {
                dsFavorMenus = favorMenus[1] as DataSet;
            }
            if (dsFavorMenus.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < dsFavorMenus.Tables[0].Rows.Count; i++)
                    if (dsFavorMenus.Tables[0].Rows[i]["GROUPNAME"].ToString() == cbGroup.Text)
                        this.lbFavor.Items.Add(dsFavorMenus.Tables[0].Rows[i]["CAPTION"].ToString());
            }
        }
    }
}